﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace FFWorker
{
    public class DeleteEmptyFolders : ActionBaseClass
    {
        String FolderName = null;

        public DeleteEmptyFolders(GroupBox GUIB, DataGridView GUIOutWindow)
            : base(GUIB, GUIOutWindow)
        {
            IsActive = false;
        }

        public void DEFChooseFolderEvent(String DefaultFolder)
        {
            FolderName = OpenFolder(DefaultFolder);

            AnalyseFolders(FolderName);
        }

        public void DEFDeleteFolderEvent()
        {
            DeleteFolders(FolderName);
        }

        private String OpenFolder(String DefaultFolder)
        {
            FolderBrowserDialog folderBrowserDialog1;

            folderBrowserDialog1 = new FolderBrowserDialog();

            // Set the help text description for the FolderBrowserDialog.
            folderBrowserDialog1.Description =
                "Select the directory that you want to use.";

            // Default to the My Documents folder.
            folderBrowserDialog1.SelectedPath = DefaultFolder;

            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                //Store default folder
                AppSettings.AppSet.DefaultDEFDirectory = folderBrowserDialog1.SelectedPath;

                return folderBrowserDialog1.SelectedPath;
            }
            else
            {
                return null;
            }
        }

        private void AnalyseFolders(String PathFolder)
        {
            if (PathFolder == null)
            {
                MessageBox.Show("Some folder must be specified", "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            GUIOutWin.Rows.Clear();

            var Dirs = Directory.EnumerateDirectories(PathFolder, "*", SearchOption.AllDirectories);

            foreach (var Dir in Dirs)
            {
                int rowIndex;

                rowIndex = GUIOutWin.Rows.Add((string)Dir);

                DirectoryInfo DirI = new DirectoryInfo(Dir);

                if (DirI.GetFileSystemInfos().Length == 0)
                {
                    GUIOutWin.Rows[rowIndex].Cells[1].Style.BackColor = Color.OrangeRed;
                    GUIOutWin.Rows[rowIndex].Cells[1].Value = Const.MATCH;
                }
            }
        }

        private void DeleteFolders(String FolderToDelete)
        {
            if (FolderToDelete == null)
            {
                MessageBox.Show("Some folder must be specified", "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }

            foreach (DataGridViewRow Row in GUIOutWin.Rows)
            {
                if ((string)Row.Cells[1].Value == Const.MATCH)
                {
                    Folder.DeleteFolder(Row.Cells[0].Value.ToString(), true);

                    Row.Cells[1].Value = "Deleted";
                }
            }
        }
    }
}
